package dao;

import java.sql.SQLException;
import java.util.List;

import entity.Goods;
import entity.Lable;
import utils.JdbcUtils;

public class LableDao {
	// {1}增加
	public void addLable(Lable lable) throws SQLException {
		String sql = "insert into lable(lname)values(?)";// ?表示占位符，填充插入占位符时表中有几列就写几
		int row = JdbcUtils.update(sql, lable.getLname());
		System.out.println("已增加" + row + "行数据");
	}

	// {2}删除
	public void deleteLable(Lable lable) throws SQLException {
		String sql = "delete from lable where gid=?";
		int row = JdbcUtils.update(sql, lable.getLid());
		System.out.println("已删除" + row + "行数据");
	}

	// {3}修改
	public void updateLable(Lable lable) throws SQLException {
		String sql = "update lable set lname=? where lid=?";
		int row = JdbcUtils.update(sql, lable.getLname(), lable.getLid());
		System.out.println("已修改" + row + "行数据");
	}

	// {4}根据标签ID查询
	public Lable queryById(Lable l) throws SQLException {
		String sql = "select * from lable l where l.lid=?";
		Lable lab = JdbcUtils.query(sql, Lable.class, l.getLid());
		return lab;
	}

	// {5}查询所有记录
	public List<Lable> queryAll() throws SQLException {
		List<Lable> list = null;
		String sql = "select * from lable l where l.lid <= ?";
		list = JdbcUtils.queryList(sql, Lable.class, 1000);
		return list;
	}
	
	//{6}通过Lable标签ID去查询商品列表
	public List<Goods> querBylIdGoods(int lid) throws SQLException{
		List<Goods> list = null;
		String sql = "select goods.gid,gname,amount,info,oldprice,price,datatime,photo,grade,lable,sale from goods_lable_info as pd join goods on pd.gid = goods.gid where pd.lid = ?";		
		list = JdbcUtils.queryList(sql, Goods.class,lid);

		return list;
	}

}
